--
--  QuickLockAppDelegate.applescript
--  QuickLock
--
--  Created by Andrew Thomson on 5/22/11.
--  Copyright 2011 ThomsonTown. All rights reserved.
--

script QuickLockAppDelegate
	property parent : class "NSObject"
    property aNotify : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		-- Time bomb for testing code
        set theStopDate to date "12-31-2011"
        set theCurrentDate to current date
        if (theStopDate - theCurrentDate) div days is less than 0 then tell current application to quit
        
        
        --  Get the current path and name of the app
        set thePath to POSIX path of (path to current application) 
        set theAppName to the name of the current application
      
        --  Get list of login applications that run automatically at login
        tell application "System Events" to set theList to (get the name of every login item)
        
        --  Check to see if QuickLock is in list of login applications
        if theList does not contain theAppName then 
            activate
            set thePrompt to display dialog "Would you like QuickLock to automatically run at login?" buttons {"No", "Yes"} default button {"Yes"} with title theAppName with icon 1
            
            --  Prompt to add QuickLook to automatically launch at login 
            if button returned of thePrompt is "Yes" then
                try
                    tell application "System Events" to make login item at end with properties {path:thePath, kind:"application", name:theAppName}
                on error
                    activate
                    display dialog "Unable to set Password Monitor to automatically launch at login." buttons {"Ok"} default button {"Ok"} with icon warning with title theAppName
                end try
            end if
        end if
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
    on mnu_quicklock_(sender)
        do shell script "'/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession' -suspend"
    end mnu_lock
    
    on mnu_screensaver_(sender)
        activate application "ScreenSaverEngine"
    end mnu_screensaver
    
    on mnu_sleep_(sender)
        tell application "Finder" to sleep
    end mnu_screensaver
    
    on mnu_shutdown_(sender)
        tell application "Finder" to shut down
    end mnu_shutdown
    
    on mnu_restart_(sender)
        tell application "Finder" to restart
    end mnu_restart
end script